//
//  TravelSubjectViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "TravelSubjectViewController.h"
#import "TravelSubjectTableViewCell.h"
#import "LSAppTools.h"
#import "TravelSubjectModel.h"
#import "PlaceDetailViewController.h"
@interface TravelSubjectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *placeArray;
@property (nonatomic, strong) NSMutableArray *nameArray;
@end

@implementation TravelSubjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"专题";
    
    UIImage *leftImage = [UIImage imageNamed:@"iconfont-fanhui-2.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction)];
    
    
    self.placeArray = [NSMutableArray array];
    self.nameArray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 80 * HEI) style:UITableViewStylePlain];
    
    [self.view addSubview:self.myTableView];

    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView registerClass:[TravelSubjectTableViewCell class] forCellReuseIdentifier:@"TravelSubjectTableViewCell"];
    NSString *url_string = [NSString stringWithFormat:@"http://chanyouji.com/api/articles.json?destination_id=%@&page=1", self.dURL];
    [LSAppTools getDataWithURL:url_string blcok:^(id result) {
        NSMutableArray *array = [NSMutableArray arrayWithArray:result];
        for (NSMutableDictionary *dic in array) {
            TravelSubjectModel *travelSubjectModel = [[TravelSubjectModel alloc] init];
            [travelSubjectModel setValuesForKeysWithDictionary:dic];
            
            if (![self.nameArray containsObject:travelSubjectModel.name]) {
                [self.placeArray addObject:travelSubjectModel];
                [self.nameArray addObject:travelSubjectModel.name];
            }
        }
        [self.myTableView reloadData];

        
    }];
}


-(void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.placeArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdenrifier = @"TravelSubjectTableViewCell";
    
    TravelSubjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdenrifier];
    
    TravelSubjectModel *travelSubjectModel = [self.placeArray objectAtIndex:indexPath.row];
    cell.travelSubject = travelSubjectModel;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 240 * HEI;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PlaceDetailViewController *placeDetailVC = [[PlaceDetailViewController alloc] init];
    placeDetailVC.hidesBottomBarWhenPushed = YES;
    TravelSubjectModel *travelSubjectModel = [self.placeArray objectAtIndex:indexPath.row];
    placeDetailVC.travelSubejctModel = travelSubjectModel;
    [self.navigationController pushViewController:placeDetailVC animated:YES];
    
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
